package com.wufeng.springcloud.service.impl;

import com.wufeng.springcloud.dao.PaymentDao;
import com.wufeng.springcloud.entities.CommonResult;
import com.wufeng.springcloud.entities.Payment;
import com.wufeng.springcloud.service.PaymentService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Service
public class PaymentServiceImpl implements PaymentService {
    @Resource
    private PaymentDao paymentDao;

    @Value("${server.port}")
    private String port;

    @Override
    public CommonResult create(Payment payment) {
        Integer result = paymentDao.create(payment);
        Long id = payment.getId();
        log.info("插入结果id : " + payment.getId());
        log.info("*****\t插入结果：" + payment);
        if (result > 0) {
            return new CommonResult(200, port + "插入成功： ", id);
        } else {
            return new CommonResult(500, port + "插入失败");
        }
    }

    @Override
    public CommonResult findAll() {
        List<Payment> list = paymentDao.findAll();
        log.info("*****查询结果：" + list);
        if (list != null) {
            return new CommonResult(200, port + "查询成功", list);
        } else {
            return new CommonResult(500, port + "查询失败");
        }
    }

    @Override
    public CommonResult findById(Long id) {
        Payment payment = paymentDao.findById(id);
        log.info("*****查询结果：" + payment);
        if (payment != null) {
            return new CommonResult(200, port + "查询成功", payment);
        } else {
            return new CommonResult(500, port + "查询失败");
        }
    }

    @Override
    public CommonResult update(Payment payment) {
        int result = paymentDao.update(payment);
        if (result > 0) {
            return new CommonResult(200, port + "更新成功", result);
        } else {
            return new CommonResult(500, port + "更新失败");
        }
    }

    @Override
    public CommonResult delete(Long id) {
        int result = paymentDao.delete(id);
        if (result > 0) {
            return new CommonResult(200, port + "删除成功", result);
        } else {
            return new CommonResult(500, port + "删除失败");
        }
    }
}
